using Agron.Data.Contracts;

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;


namespace Agron.Services.SessionController
{
    /// <summary>
    /// An <see cref="IList{SessionMember}"/> collection.
    /// </summary>
    [Serializable]
    public sealed class SessionMemberCollection : IList<SessionMember>
    {
        private readonly List<SessionMember> _innerList;
        private readonly static object _lockObject = new object();

        /// <summary>
        /// Initializes a new instance of the <see cref="SessionMemberCollection"/> class.
        /// </summary>
        public SessionMemberCollection()
        {
            this._innerList = new List<SessionMember>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SessionMemberCollection"/> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.
        /// </summary>
        /// <param name="collection">The collection whose elements are copied to the new list.</param>
        public SessionMemberCollection(IEnumerable<SessionMember> collection)
        {
            this._innerList = new List<SessionMember>(collection);
        }

        /// <summary>
        /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"></see> contains an item with the specified user id.
        /// </summary>
        /// <param name="userId">A <see cref="Guid"/> that uniquely identifies a user.</param>
        /// <returns>
        /// true if item is found in the <see cref="T:System.Collections.Generic.ICollection`1"></see>; otherwise, false.
        /// </returns>
        public bool Contains(Guid userId)
        {
            lock (_lockObject)
            {
                foreach (SessionMember item in this._innerList)
                {
                    if (userId == item.UserId)
                        return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Gets or sets the <see cref="Agron.Data.Contracts.SessionMember"/> with the specified user id.
        /// </summary>
        /// <value></value>
        public SessionMember this[Guid userId]
        {
            get
            {
                lock (_lockObject)
                {
                    for (int index = 0; index < this._innerList.Count; index++)
                    {
                        if (userId == this._innerList[index].UserId)
                            return this[index];
                    }
                }

                return null;
            }
            set
            {
                lock (_lockObject)
                {
                    for (int index = 0; index < this._innerList.Count; index++)
                    {
                        if (userId == this._innerList[index].UserId)
                        {
                            this[index] = value;
                            return;
                        }
                    }
                }

                throw new ArgumentOutOfRangeException(Properties.Resources.MemberListManagerUserNotFound);
            }
        }

        /// <summary>
        /// Returns a read-only <see cref="ReadOnlyCollection{SessionMember}"/> wrapper for the current collection.
        /// </summary>
        /// <returns>
        /// A <see cref="ReadOnlyCollection{SessionMember}"/> that acts as a read-only wrapper around the current <see cref="SessionMemberCollection"/>.
        /// </returns>
        public ReadOnlyCollection<SessionMember> AsReadOnly()
        {
            return this._innerList.AsReadOnly();
        }

        /// <summary>
        /// Removes the <see cref="SessionMember"/> that matches the specified user id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        public void RemoveUser(Guid userId)
        {
            int targetIndex = -1;

            lock (_lockObject)
            {
                for (int index = 0; index < this._innerList.Count; index++)
                {
                    if (userId == this._innerList[index].UserId)
                    {
                        targetIndex = index;
                        break;
                    }
                }

                if (-1 < targetIndex)
                    this.RemoveAt(targetIndex);
            }
        }

        #region IList<SessionMember> Members

        /// <summary>
        /// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1"></see>.
        /// </summary>
        /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.IList`1"></see>.</param>
        /// <returns>
        /// The index of item if found in the list; otherwise, -1.
        /// </returns>
        public int IndexOf(SessionMember item)
        {
            return this._innerList.IndexOf(item);
        }

        /// <summary>
        /// Inserts an item to the <see cref="T:System.Collections.Generic.IList`1"></see> at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which item should be inserted.</param>
        /// <param name="item">The object to insert into the <see cref="T:System.Collections.Generic.IList`1"></see>.</param>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1"></see> is read-only.</exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"></see>.</exception>
        public void Insert(int index, SessionMember item)
        {
            this._innerList.Insert(index, item);
        }

        /// <summary>
        /// Removes the <see cref="T:System.Collections.Generic.IList`1"></see> item at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index of the item to remove.</param>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1"></see> is read-only.</exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"></see>.</exception>
        public void RemoveAt(int index)
        {
            this._innerList.RemoveAt(index);
        }

        /// <summary>
        /// Gets or sets the <see cref="Agron.Data.Contracts.SessionMember"/> at the specified index.
        /// </summary>
        /// <value></value>
        public SessionMember this[int index] { get { return this._innerList[index]; } set { this._innerList[index] = value; } }

        #endregion

        #region ICollection<SessionMember> Members

        /// <summary>
        /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
        /// </summary>
        /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception>
        public void Add(SessionMember item)
        {
            this._innerList.Add(item);
        }

        /// <summary>
        /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
        /// </summary>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only. </exception>
        public void Clear()
        {
            this._innerList.Clear();
        }

        /// <summary>
        /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"></see> contains a specific value.
        /// </summary>
        /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
        /// <returns>
        /// true if item is found in the <see cref="T:System.Collections.Generic.ICollection`1"></see>; otherwise, false.
        /// </returns>
        public bool Contains(SessionMember item)
        {
            return this._innerList.Contains(item);
        }

        /// <summary>
        /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"></see> to an <see cref="T:System.Array"></see>, starting at a particular <see cref="T:System.Array"></see> index.
        /// </summary>
        /// <param name="array">The one-dimensional <see cref="T:System.Array"></see> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"></see>. The <see cref="T:System.Array"></see> must have zero-based indexing.</param>
        /// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">arrayIndex is less than 0.</exception>
        /// <exception cref="T:System.ArgumentNullException">array is null.</exception>
        /// <exception cref="T:System.ArgumentException">array is multidimensional.-or-arrayIndex is equal to or greater than the length of array.-or-The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"></see> is greater than the available space from arrayIndex to the end of the destination array.-or-Type T cannot be cast automatically to the type of the destination array.</exception>
        public void CopyTo(SessionMember[] array, int arrayIndex)
        {
            this._innerList.CopyTo(array, arrayIndex);
        }

        /// <summary>
        /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
        /// </summary>
        /// <value></value>
        /// <returns>The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</returns>
        public int Count { get { return this._innerList.Count; } }

        /// <summary>
        /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only; otherwise, false.</returns>
        public bool IsReadOnly { get { return false; } }

        /// <summary>
        /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
        /// </summary>
        /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
        /// <returns>
        /// true if item was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"></see>; otherwise, false. This method also returns false if item is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"></see>.
        /// </returns>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception>
        public bool Remove(SessionMember item)
        {
            return this._innerList.Remove(item);
        }

        #endregion

        #region IEnumerable<SessionMember> Members

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator<SessionMember> GetEnumerator()
        {
            return this._innerList.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
        /// </returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this._innerList.GetEnumerator();
        }

        #endregion
    }
}
